/************************************************************
 *  * Hyphenate CONFIDENTIAL
 * __________________
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 *
 * NOTICE: All information contained herein is, and remains
 * the property of Hyphenate Inc.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Hyphenate Inc.
 */

#import "EaseBubbleView+Text.h"

@implementation EaseBubbleView (Text)

#pragma mark - private

- (void)_setupTextBubbleMarginConstraints
{
    NSLayoutConstraint *marginTopConstraint = [NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeTop multiplier:1.0 constant:self.margin.top];
    NSLayoutConstraint *marginBottomConstraint = [NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-self.margin.bottom];
    NSLayoutConstraint *marginLeftConstraint = [NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-self.margin.right];
    NSLayoutConstraint *marginRightConstraint = [NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:self.margin.left];
    
    [self.marginConstraints removeAllObjects];
    [self.marginConstraints addObject:marginTopConstraint];
    [self.marginConstraints addObject:marginBottomConstraint];
    [self.marginConstraints addObject:marginLeftConstraint];
    [self.marginConstraints addObject:marginRightConstraint];
    
    [self addConstraints:self.marginConstraints];
}

- (void)_setupTextBubbleConstraints
{
    [self _setupTextBubbleMarginConstraints];
}

#pragma mark - public

- (void)setupTextBubbleView
{
    self.backgroundImageView.userInteractionEnabled = YES;
    self.userInteractionEnabled = YES;
    self.textLabel = [[UILabel alloc] init];
    self.textLabel.accessibilityIdentifier = @"text_label";
    self.textLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.textLabel.backgroundColor = [UIColor clearColor];
    self.textLabel.numberOfLines = 0;
    [self.backgroundImageView addSubview:self.textLabel];
    
    [self _setupTextBubbleConstraints];
    
    self.giftView = [[UIImageView alloc] init];
    self.giftView.contentMode = UIViewContentModeScaleAspectFit;
    self.giftView.clipsToBounds = YES;
    [self.backgroundImageView addSubview:self.giftView];
    
    self.giftNameLabel = [[UILabel alloc] init];
    self.giftNameLabel.font = [UIFont systemFontOfSize:12];
    self.giftNameLabel.textColor = HOTCHATrgb(51, 51, 51);
    [self.backgroundImageView addSubview:self.giftNameLabel];
    
    self.vLineView = [[UIView alloc] init];
    self.vLineView.backgroundColor = HOTCHATrgb(230,230,230);
    [self.backgroundImageView addSubview:self.vLineView];
    
    self.kingLabel = [[UILabel alloc] init];
    self.kingLabel.font = [UIFont systemFontOfSize:12];
    self.kingLabel.textColor = HOTCHATrgb(102,102,102);
    [self.backgroundImageView addSubview:self.kingLabel];
    
    self.giftButton = [[UIButton alloc] init];
    [self.giftButton addTarget:self action:@selector(addMsgAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.backgroundImageView addSubview:self.giftButton];
    
    //gift only
    self.goBackView = [[UIView alloc] init];
    self.goBackView.backgroundColor = [UIColor whiteColor];
    self.goBackView.layer.cornerRadius = 20;
    self.goBackView.layer.borderWidth = 1;
    self.goBackView.layer.borderColor = HOTCHATrgb(230, 230, 230).CGColor;
    [self addSubview:self.goBackView];
    self.goImageView = [[UIImageView alloc] init];
    self.goImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.goBackView addSubview:self.goImageView];
    
    
    [self.giftView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backgroundImageView).offset(10);
        make.centerX.equalTo(self.backgroundImageView);
        make.width.height.mas_equalTo(80);
    }];
    
    [self.giftNameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.backgroundImageView);
        make.top.equalTo(self.giftView.mas_bottom).offset(5);
    }];
    
    [self.vLineView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.backgroundImageView);
        make.height.mas_equalTo(0.5);
        make.bottom.equalTo(self.backgroundImageView).offset(-45);
    }];
    
    [self.kingLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.backgroundImageView).offset(-15);
        make.centerY.equalTo(self.vLineView.mas_top).offset(-20);
    }];
    
    [self.goBackView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backgroundImageView);
        make.centerX.equalTo(self.backgroundImageView);
        make.width.height.mas_equalTo(100);
    }];
    [self.goImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.backgroundImageView);
        make.width.height.mas_equalTo(70);
    }];
}

- (void)updateTextMargin:(UIEdgeInsets)margin
{
    if (_margin.top == margin.top && _margin.bottom == margin.bottom && _margin.left == margin.left && _margin.right == margin.right) {
        return;
    }
    _margin = margin;
    
    [self removeConstraints:self.marginConstraints];
    [self _setupTextBubbleMarginConstraints];
}
- (void)updateGiftTextMargin:(UIEdgeInsets)margin{
    if (_margin.top == margin.top && _margin.bottom == margin.bottom && _margin.left == margin.left && _margin.right == margin.right) {
        return;
    }
    _margin = margin;
    
    [self removeConstraints:self.marginConstraints];
    
    NSLayoutConstraint *marginTopConstraint = [NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeTop multiplier:1.0 constant:self.margin.top];
    NSLayoutConstraint *marginBottomConstraint = [NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-self.margin.bottom];
    NSLayoutConstraint *marginLeftConstraint = [NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-self.margin.right];
    NSLayoutConstraint *marginRightConstraint = [NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:self.margin.left];
    
    [self.marginConstraints removeAllObjects];
    [self.marginConstraints addObject:marginTopConstraint];
    [self.marginConstraints addObject:marginBottomConstraint];
    [self.marginConstraints addObject:marginLeftConstraint];
    [self.marginConstraints addObject:marginRightConstraint];
    
    [self addConstraints:self.marginConstraints];
    
}

- (void)HOTCHATsetCustMode:(NSDictionary *)ext type:(NSInteger)msgType isSender:(BOOL)isSender{
    self.giftButton.hidden = YES;
    self.vLineView.hidden = YES;
    self.kingLabel.text = nil;
    self.giftView.image = nil;
    self.giftButton.tag = msgType;
    self.goBackView.hidden = YES;
    self.backgroundImageView.hidden = NO;
    switch (msgType) {
        case 0:{
            self.giftView.image = nil;
            self.giftNameLabel.text = nil;
        }
            break;
//        case HOTCHATCMT_Gift:{
//            self.textLabel.text = nil;
//            HOTCHATGiftManage *ggmodel = [HOTCHATGiftManage HOTCHATinitWithDic:[ext valueForKey:[HOTCHATStringUtils getThegift]]];
//            self.backgroundImageView.hidden = YES;
//            self.goBackView.hidden = NO;
//            [self.goImageView sd_setImageWithURL:[ggmodel.address HOTCHATfullURL] placeholderImage:[UIImage imageNamed:[HOTCHATStringUtils getTheblue]]];
//            self.giftNameLabel.text = nil;//[NSLocalizedString([HOTCHATStringUtils getTheStringNO210], nil) stringByAppendingString:ggmodel.name];
//            //            [self.goImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
//            //                make.top.equalTo(self.backgroundImageView).offset(10);
//            //                make.centerX.equalTo(self.backgroundImageView);
//            //                make.width.height.mas_equalTo(80);
//            //            }];
//            //
//            //            [self.giftNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//            //                make.centerX.equalTo(self.backgroundImageView);
//            //                make.top.equalTo(self.giftView.mas_bottom).offset(5);
//            //            }];
//        }
//            break;
        case HOTCHATCMT_Voice:{
            self.giftNameLabel.text = NSLocalizedString(@"StrN051", nil);
            self.textLabel.text = nil;
            self.vLineView.hidden = NO;
//            NSString *kid = [ext valueForKey:HOTCHATCMT_GoldCoin];
//            self.kingLabel.text = [NSString stringWithFormat:NSLocalizedString([HOTCHATStringUtils getTheStringNO122], nil),HOTCHATvoicePriceA(kid)];
//            self.giftButton.hidden = NO;
//            [self.giftButton setTitle:NSLocalizedString([HOTCHATStringUtils getTheStringNO216], nil) forState:(UIControlStateNormal)];
//            [self.giftButton setTitleColor:HOTCHATrgb(26,26,26) forState:(UIControlStateNormal)];
//            self.giftButton.titleLabel.font = [UIFont systemFontOfSize:15];
//            [self.giftButton mas_remakeConstraints:^(MASConstraintMaker *make) {
//                make.top.equalTo(self.vLineView.mas_bottom);
//                make.bottom.left.right.equalTo(self.backgroundImageView);
//            }];
//            [self.giftNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//                make.top.left.equalTo(self.backgroundImageView).offset(15);
//            }];
        }
            
            break;
        case HOTCHATCMT_Video:{
//            self.giftNameLabel.text = NSLocalizedString([HOTCHATStringUtils getTheStringNO234], nil);
//            self.textLabel.text = nil;
//            self.vLineView.hidden = NO;
//            NSString *kid = [ext valueForKey:HOTCHATCMT_GoldCoin];
//            self.kingLabel.text = [NSString stringWithFormat:NSLocalizedString([HOTCHATStringUtils getTheStringNO122], nil),HOTCHATvideoPriceA(kid)];
//            self.giftButton.hidden = NO;
//            [self.giftButton setTitle:NSLocalizedString([HOTCHATStringUtils getTheStringNO216], nil) forState:(UIControlStateNormal)];
//            [self.giftButton setTitleColor:HOTCHATrgb(26,26,26) forState:(UIControlStateNormal)];
//            self.giftButton.titleLabel.font = [UIFont systemFontOfSize:15];
//            [self.giftButton mas_remakeConstraints:^(MASConstraintMaker *make) {
//                make.top.equalTo(self.vLineView.mas_bottom);
//                make.bottom.left.right.equalTo(self.backgroundImageView);
//            }];
//            [self.giftNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//                make.top.left.equalTo(self.backgroundImageView).offset(15);
//            }];
        }
            break;
        case HOTCHATCMT_G_MG:{
//            HOTCHATGiftManage *gmg = [GIFTMANAGE HOTCHATgetGiftByGID:@"11"];
//            self.giftButton.hidden = isSender;
//            self.textLabel.text = nil;
//            self.giftNameLabel.text = NSLocalizedString([HOTCHATStringUtils getTheStringNO207], nil);
//            [self.giftButton setTitle:NSLocalizedString([HOTCHATStringUtils getTheStringNO209], nil) forState:(UIControlStateNormal)];
//            [self.giftButton setTitleColor:HOTCHATrgb(102,102,102) forState:(UIControlStateNormal)];
//            self.giftButton.titleLabel.font = [UIFont systemFontOfSize:12];
//            [self.giftButton mas_remakeConstraints:^(MASConstraintMaker *make) {
//                make.height.mas_equalTo(40);
//                make.left.equalTo(self.giftNameLabel);
//                make.bottom.equalTo(self);
//            }];
//            [self.giftView mas_remakeConstraints:^(MASConstraintMaker *make) {
//                make.top.equalTo(self.backgroundImageView).offset(15);
//                make.bottom.equalTo(self.backgroundImageView).offset(-15);
//                make.right.equalTo(self.backgroundImageView).offset(-15);
//                make.width.mas_equalTo(50);
//            }];
//
//            [self.giftNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//                make.top.left.equalTo(self.backgroundImageView).offset(15);
//            }];
//            [self.giftView sd_setImageWithURL:[gmg.address HOTCHATfullURL] placeholderImage:[UIImage imageNamed:[HOTCHATStringUtils getTheblue]]];
        }
            break;
        case HOTCHATCMT_G_QQ:{
//            HOTCHATGiftManage *gmg = [GIFTMANAGE HOTCHATgetGiftByGID:@"20"];
//            self.giftButton.hidden = isSender;
//            self.textLabel.text = nil;
//            self.giftNameLabel.text = NSLocalizedString([HOTCHATStringUtils getTheStringNO208], nil);
//            [self.giftButton setTitle:NSLocalizedString([HOTCHATStringUtils getTheStringNO209], nil) forState:(UIControlStateNormal)];
//            [self.giftButton setTitleColor:HOTCHATrgb(102,102,102) forState:(UIControlStateNormal)];
//            self.giftButton.titleLabel.font = [UIFont systemFontOfSize:12];
//            [self.giftButton mas_remakeConstraints:^(MASConstraintMaker *make) {
//                make.height.mas_equalTo(40);
//                make.left.equalTo(self.giftNameLabel);
//                make.bottom.equalTo(self);
//            }];
//            [self.giftView mas_remakeConstraints:^(MASConstraintMaker *make) {
//                make.top.equalTo(self.backgroundImageView).offset(15);
//                make.bottom.equalTo(self.backgroundImageView).offset(-15);
//                make.right.equalTo(self.backgroundImageView).offset(-15);
//                make.width.mas_equalTo(50);
//            }];
//            
//            [self.giftNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//                make.top.left.equalTo(self.backgroundImageView).offset(15);
//            }];
//            [self.giftView sd_setImageWithURL:[gmg.address HOTCHATfullURL] placeholderImage:[UIImage imageNamed:[HOTCHATStringUtils getTheblue]]];
        }
            break;
            
        default:
            break;
    }
    
}

@end
